<?php



class Sve_Funkcije {
	
	function __construct() {
		$this->setCounter();
	}
	
	
	public $txt;
	
	private $ended;
	
	private $started;
	
	private $sessionNumber;
	
	private $rememberCount;
	
	private $maxAttempts = 5;
	
	public $_SESSION;
	
	public $counter;
	
	private $arrayAttemptedNumbers;
	
	private $checker;
	
	private $showGuessingForm = true;
	
	private $randomNumberFrom = 1;
	
	private $randomNumberTo = 10;
	
	private $number;
	
	private $showStats = false;
	
	private $numRange = 3;
	
	/**
	 * @return int
	 */
	public function getStarted() {
		return $this->started;
	}
	/**
	 * @return int
	 */
	public function getEnded() {
		return $this->ended;
	}
	/**
	 * @return int
	 */
	public function getSessionNumber() {
		return $this->sessionNumber;
	}
	/**
	 * @return int
	 */
	public function getTimeSpent() {
		return $this->timeSpent;
	}
	/**
	 * @return int
	 */
	public function getRememberCount() {
		return $this->rememberCount;
	}
	/**
	 * @return array
	 */
	public function getAttemptedNumbers() {
		return $this->attemptedNumbers();
	}
	/**
	 * @param bool
	 */
	public function setShowStats($value) {
		$this->showStats = $value;
	}
	/**
	 * @return mixed
	 */
	public function getShowStats() {
		return $this->showStats;
	}
	/**
	 * @param bool
	 */
	public function setClose($value) {
		$this->close = $value;
	}
	/**
	 * @return bool
	 */
	public function getClose() {
		return $this->close;
	}
	/**
	 * @return bool
	 */
	public function getChecker() {
		return $this->checker;
	}
	/**
	 * @return int
	 */
	public function getRandomNumberFrom () {
		return $this->randomNumberFrom;
	}
	/**
	 * @return int
	 */
	public function getRandomNumberTo() {
		return $this->randomNumberTo;
	}
	/**
	 * @return int
	 */
	public function getMaxAttempts() {
		return $this->maxAttempts;
	}
	/**
	 * @return int
	 */
	public function getNumber() {
		return $this->number;
	}
	/**
	 * @return bool
	 */
	public function getShowGuessingForm() {
		return $this->showGuessingForm;
	}
	/**
	 * @param bool
	 */
	public function setShowGuessingForm($value) {
		$this->showGuessingForm = $value;
	}
	/**
	 * @param bool
	 */
	public function setChecker($value) {
		$this->checker = $value;
	}
	/**
	 * @return int
	 */
	public function getNumRange() {
		return $this->numRange;
	}
	/**
	 * @return int
	 */
	public function getCounter() {
		return $this->counter;
	}
	/**
	 * @param int
	 */
	public function setNumber($number) {
		$this->number = $number;
	}

	/**
	 * @return the $arrayAttemptedNumbers
	 */
	public function getArrayAttemptedNumbers() {
		return $this->arrayAttemptedNumbers;
	}

	/**
	 * @return the $close_range
	 */
	public function getClose_range() {
		return $this->close_range;
	}

	/**
	 * @return the $closenes
	 */
	public function getClosenes() {
		return $this->closenes;
	}

	/**
	 * @param int $ended
	 */
	public function setEnded($ended) {
		$this->ended = $ended;
	}

	/**
	 * @param int $started
	 */
	public function setStarted($started) {
		$this->started = $started;
	}

	/**
	 * @param int $sessionNumber
	 */
	public function setSessionNumber($sessionNumber) {
		$this->sessionNumber = $sessionNumber;
	}

	/**
	 * @param int $rememberCount
	 */
	public function setRememberCount($rememberCount) {
		$this->rememberCount = $rememberCount;
	}

	/**
	 * @param array $arrayAttemptedNumbers
	 */
	public function setArrayAttemptedNumbers($arrayAttemptedNumbers) {
		$this->arrayAttemptedNumbers = $arrayAttemptedNumbers;
	}

	/**
	 * @param int, int $timeSpent
	 */
	public function setTimeSpent($beginning_time, $ending_time) {
		// Convert the ending time (its the parameter) to seconds
		$ending_time = strtotime($ending_time);
		// Convert the starting time to seconds  (this is also a parameter)
		$beginning_time = strtotime($beginning_time);
		
		// Finaly set the time spent as the subtraction of begining time from ending time
		$this->timeSpent = $ending_time - $beginning_time;
	}
	
	
	/**
	 * @param Ambigous <number, NULL, unknown> $counter
	 */
	public function setCounter() {
		
		// Set the counter as the number we stored in the session the last time. If there is no data in the session, it is 0
		$this->counter = isset($_SESSION['remember_count']) ? $_SESSION['remember_count'] : 0;
		
		// Then we are incrementing it by one, so its now one more then the previous time we guessed
		$this->counter++;
		
		// And finally we are writing the new number into a session to be used the next time we guess
		$_SESSION['remember_count'] = $this->counter;
	}

	/**
	 * @param int $close_range
	 */
	public function setClose_range($close_range) {
		$this->close_range = $close_range;
	}

	/**
	 * @param bool $closenes
	 */
	public function setClosenes($closenes) {
		$this->closenes = $closenes;
	}
	

	
	
	
	
	// Set a couple of properties by using session data. ALL OF THIS IS USED IN stats() METHOD
	function initializeStats() {
		// Set the time the game was started, retrieve it from the session
		$this->started = $_SESSION['started'];
		
		// Set the ending time as current time
		$this->setEnded(date('H:i:s'));
		
		// Set the sessionNumber as the number we stored in session
		$this->sessionNumber = $_SESSION['number'];
		
		// The rememberCount is a counter of attempts
		$this->rememberCount = $_SESSION['remember_count'];
	  	
		// Call the setTimeSpent() method to see how much time passed between the beginning and ending
	  	$this->setTimeSpent($this->getStarted(), $this->getEnded());
	}
	

	
	// Function to find attempted numbers
	function attemptsNumbersList() {
		
		// Get the attempted numbers list from the session
		$this->arrayAttemptedNumbers = $_SESSION['attempted_numbers'];
		
		// Print all attempted numbers from the session
		foreach($this->arrayAttemptedNumbers as $key => $value) {
			
			// Print the number
			print '<td align="center">' . $value . '</td>';
		}
	}
	
	// Find out when the attempts were made
	function attemptsNumbersTime() {
		
		// Use the for loop to find array keys
		for($i = 1; $i <= $this->getMaxAttempts(); $i++) {
			
			// If the time from the session is empty, do not run the code.
			if(!empty($_SESSION['attempt'][$i]['time'])) {
			
			// Make another array. This will use the number of each attempt and use it like an index for the array. We specified this bellow with $counter
			$value_array = array($_SESSION['attempt'][$i]['time']);
		
				// Now we use the indexes of the array to find the time itself
				foreach ($value_array as $index => $print_time) {
					
					// And we print the time, finally
					echo '<td>'.$print_time.'</td>';
				}
			}
		}
	}

	function mechanismCheck($getAttempt, $theNumber) {
		if ($getAttempt == $theNumber) {
			return true;
		} else {
			return false;
		}
	}

	// This is the help function. It outputs "That was close" and "Try a bigger number"
	function help($chosen_num, $attempted_num) {
	
	// If the chosen number is bigger than the attempted number, that means user needs to try a bigger number
		if($chosen_num > $attempted_num) {
		  
	  		// So set the checker to true.
			$this->setChecker(true);
		  
		  	// And if the difference between the chosen number and the attempted number is smaller than 3 ($range is equal 3).....
		  	if(($chosen_num - $attempted_num) < $this->getNumRange()) {
		  		// Set the Close property to true
				$this->setClose(true);
		  	}
		  	// Otherwise
		  	else {
		  		// false
			  	$this->setClose(false);
		  	}
	  	}
	  	// But if the attempted number is bigger than the chosen number
	  	else {
		  	// then the Checker is false
	  		$this->setChecker(false);
	  		
		  	// Then we determine the range again
		  	if(($attempted_num - $chosen_num) < $this->getNumRange()) {
				// If the range is bigger than the difference of the two numbers, then the user was CLOSE
		  		$this->setClose(true);
		  	}
		  	else {
		  		// Otherwise, he wasn't close, so it's false
			  	$this->setClose(false);
		  	}
	  	}	
	}
	


  	// A function for creating a random number
  	function createRandomNumber()
  	{
		// Generate a random number and assign it to $number property
  		$this->setNumber(rand($this->getRandomNumberFrom() , $this->getRandomNumberTo()));
  	
  		// Save that number in a session
  		$_SESSION['number'] = $this->getNumber();
  	
  	}
  	 
  	
}

?>